/*  

Cafechess Library: Cover basic chess programming needs, like GUI, Engine integration, 
FEN and PGN parsing.

                 \\|//
                 (o o)
--------------ooO-(_)-Ooo----------------------------------
Copyright (C) 2009  By Gregory A. Prentice Snoqualmie, WA
                    www.cafechess.org
                    cafechess@comcast.net
                      Oooo
--------------oooO----(  )---------------------------------
              (  )    (_/
               \_)

Microsoft Public License (Ms-PL)
               
Revisions:

Author        Date    Description:

*/
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using Cafechess.Chess.Parsers;

namespace Cafechess.Control.Chessboard.Demo
{
  /// <summary>
  /// Summary description for PGNtoRTF.
  /// </summary>
  public class PGNtoRTF : IGameParserEvents
  {  
    int count = 0;
    int depth = 0;
    System.Windows.Forms.RichTextBox coRichText;
    private Stack coSaveState;

    public PGNtoRTF(System.Windows.Forms.RichTextBox richText)
    {
      coRichText = richText;
      coRichText.Clear();
      setFont();
      coSaveState = new Stack();


      /*  
            coRichText.SelectedText = "AAAAA\n";
            string text = coRichText.SelectedRtf;
            int startpos = text.IndexOf(@"\uc1") + 5;
            int nextpos = text.IndexOf(@"\pard\") + 6;

            text = text.Insert(startpos, @"hello next is hidden: \v m.1.2 \v0 next is not hidden: aaaa");
            coRichText.SelectedRtf = text;

            nextpos = coRichText.Find("m.1.2", System.Windows.Forms.RichTextBoxFinds.None);
            //coRichText.SelectedRtf = text;

            //coRichText.SelectedRtf =  @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033\uc1 }ME ME ME\r\n\0" ;// RTF_DOCUMENT_PRE + "my new data" + RTF_DOCUMENT_POST;
            //coRichText.SelectionFont = new Font( "Tahoma", 10, System.Drawing.FontStyle.Bold );
      */  
      count = 0;
    }
    public void NewGame(IGameParser iParser)
    {
      lastState = iParser.State;
      count++;
    }

    public void ExitHeader(IGameParser iParser)
    {
    }

    public void EnterVariation(IGameParser iParser)
    {
      coSaveState.Push( moveNumber );

      lastState = iParser.State;
      
      depth++;
    }
    Font mainFont = new Font( "Courier New", 12, System.Drawing.FontStyle.Bold);
    Font varFont  = new Font( "Courier New", 8, System.Drawing.FontStyle.Italic );

    void setFont()
    {
      if( depth == 0 )
        coRichText.SelectionFont = mainFont;
      else
        coRichText.SelectionFont = varFont;
    }
    public void ExitVariation(IGameParser iParser)
    {
      MoveParsed(iParser);
      lastState = iParser.State;
      depth--;
      moveNumber = (int)coSaveState.Pop();

      // coRichText.SelectionIndent = depth * 20;
    }

    public void Starting(IGameParser iParser)
    {
    }
    public void Finished(IGameParser iParser)
    {
      lastState = iParser.State;
      coSaveState.Clear();
      //coRichText.SaveFile("c:/temp/Polgar-Anand.rtf");
    }

    public void TagParsed(IGameParser iParser)
    {
      lastState = iParser.State;
      drawText(iParser.Tag + ": ", Color.Black);
      drawText(iParser.Value + "\n", Color.Black);
    }
    public void CommentParsed(IGameParser iParser)
    {
      lastState = iParser.State;
      drawText(iParser.Value, Color.Green);
      drawText(" ", Color.Black);
    }
    public void NagParsed(IGameParser iParser)
    {
      lastState = iParser.State;
    }
    GameParserState.State lastState = GameParserState.State.HEADER;
    int moveNumber=0;
    public void MoveParsed(IGameParser iParser)
    {
      try
      {        
        if( iParser.State == GameParserState.State.NUMBER )
        {

          // Checks for cases where someone has entered a move as: 1. e4 1...e5
          //  as we only save out our element when we have complete move list or
          //  we are entering a variation.
          moveNumber = int.Parse(iParser.Value);
        }
        else if( iParser.State == GameParserState.State.WHITE )
        {
          drawText(moveNumber.ToString() + ". " , Color.Black);
          drawText(iParser.Value + " ", Color.Blue);
        }
        else if( iParser.State == GameParserState.State.BLACK )
        {
          // Something else was parsed so we need to add blacks move number
          if( lastState != GameParserState.State.WHITE )
          {
            drawText(moveNumber.ToString() + "... " , Color.Black);
            drawText(iParser.Value + "  ", Color.Blue);
          }
          else
          {
            drawText(iParser.Value + "  ", Color.Blue);
          }
        }
      }
      catch
      {

      }
      lastState = iParser.State;
    }

    public void EndMarker(IGameParser iParser)
    {
      lastState = iParser.State;
      drawText(iParser.Value, Color.Red);
      drawText(" ", Color.Black);
    }

    void drawText(string text, Color highlite)
    {
      setFont();
      coRichText.SelectionColor = highlite;
      coRichText.SelectedText = text;
    }

  }

}
